package com.baozun.houji.me.fragment

import android.os.Bundle
import android.widget.RadioGroup
import androidx.fragment.app.Fragment
import com.baozhun.mall.common.base.BaseFragment
import com.baozun.houji.me.R
import com.baozun.houji.me.databinding.FragmentCollectionBinding
import com.baozun.houji.me.viewmodel.CollectionViewModel
import com.chad.library.adapter.base.module.DraggableModule

/**
 * @Title：我的收藏activity
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/12
 * @Version：1.0.0
 */
class MyCollectionFragment : BaseFragment<CollectionViewModel, FragmentCollectionBinding>(), DraggableModule,
    RadioGroup.OnCheckedChangeListener {
    private var mCurrentFragment: Fragment? = null
    private val mBuyByMoneyFragment by lazy { MyCollectionChannelFragment.newInstance() }
    private val mIntegralExchangeFragment by lazy { MyCollectionSelfFragment.newInstance() }

    companion object {
        fun newInstance(): MyCollectionFragment {
            return MyCollectionFragment().apply {

            }
        }

    }

    override fun initView(savedInstanceState: Bundle?) {
        mDataBinding.rgNavigation.setOnCheckedChangeListener(this)
        changeFragment(mBuyByMoneyFragment)
    }


    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        when (checkedId) {
            R.id.rb_buy -> {
                mDataBinding.rbBuy.setTextAppearance(com.baozhun.mall.common.R.style.TextBold)
                mDataBinding.rbIntegralExchange.setTextAppearance(com.baozhun.mall.common.R.style.TextNormal)
                changeFragment(mBuyByMoneyFragment)
            }

            R.id.rb_integral_exchange -> {
                mDataBinding.rbBuy.setTextAppearance(com.baozhun.mall.common.R.style.TextNormal)
                mDataBinding.rbIntegralExchange.setTextAppearance(com.baozhun.mall.common.R.style.TextBold)
                changeFragment(mIntegralExchangeFragment)
            }
        }

    }

    private fun changeFragment(fragment: Fragment) {
        var fragmentTransaction = childFragmentManager.beginTransaction()
        if (mCurrentFragment != null) {
            fragmentTransaction.hide(mCurrentFragment!!)
        }
        if (fragment.isAdded) {
            fragmentTransaction.show(fragment)
        } else {
            fragmentTransaction.add(R.id.fl_container, fragment)
        }
        fragmentTransaction.commitAllowingStateLoss()
        mCurrentFragment = fragment
    }
}